#!/usr/bin/env python

Import("env")

env.editor_sources = []

import glob
import os

import editor_builders

import methods

if env.editor_build:
    # Generate doc data paths
    def doc_data_class_path_builder(target, source, env):
        paths = dict(sorted(source[0].read().items()))
        data = "\n".join([f'\t{{"{key}", "{value}"}},' for key, value in paths.items()])
        with methods.generated_wrapper(target) as file:
            file.write(
                f"""\
static const int _doc_data_class_path_count = {len(paths)};

struct _DocDataClassPath {{
	const char *name;
	const char *path;
}};

static const _DocDataClassPath _doc_data_class_paths[{len(env.doc_class_path) + 1}] = {{
{data}
	{{nullptr, nullptr}},
}};
"""
            )

    env.CommandNoCache("doc_data_class_path.gen.h", env.Value(env.doc_class_path), env.Run(doc_data_class_path_builder))

    # Register exporters
    def register_exporters_builder(target, source, env):
        platforms = source[0].read()
        exp_inc = "\n".join([f'#include "platform/{p}/export/export.h"' for p in platforms])
        exp_reg = "\n".join([f"\tregister_{p}_exporter();" for p in platforms])
        exp_type = "\n".join([f"\tregister_{p}_exporter_types();" for p in platforms])
        with methods.generated_wrapper(target) as file:
            file.write(
                f"""\
#include "register_exporters.h"

{exp_inc}

void register_exporters() {{
{exp_reg}
}}

void register_exporter_types() {{
{exp_type}
}}
"""
            )

    gen_exporters = env.CommandNoCache(
        "register_exporters.gen.cpp", env.Value(env.platform_exporters), env.Run(register_exporters_builder)
    )
    for e in env.platform_exporters:
        # Add all .cpp files in export folder
        env.add_source_files(env.editor_sources, f"../platform/{e}/export/*.cpp")

    # Core API documentation.
    docs = []
    docs += Glob("#doc/classes/*.xml")

    # Module API documentation.
    module_dirs = []
    for d in env.doc_class_path.values():
        if d not in module_dirs:
            module_dirs.append(d)

    for d in module_dirs:
        if not os.path.isabs(d):
            docs += Glob("#" + d + "/*.xml")  # Built-in.
        else:
            docs += Glob(d + "/*.xml")  # Custom.

    docs = sorted(docs)
    env.Depends("#editor/doc_data_compressed.gen.h", docs)
    env.CommandNoCache(
        "#editor/doc_data_compressed.gen.h",
        docs,
        env.Run(editor_builders.make_doc_header),
    )

    # Editor interface and class reference translations incur a significant size
    # cost for the editor binary (see godot-proposals#3421).
    # To limit it, we only include translations with a high enough completion
    # ratio (20% for the editor UI, 10% for the class reference).
    # Generated with `make include-list` for each resource.

    # Editor translations
    tlist = glob.glob(env.Dir("#editor/translations/editor").abspath + "/*.po")
    env.Depends("#editor/editor_translations.gen.h", tlist)
    env.CommandNoCache(
        "#editor/editor_translations.gen.h",
        tlist,
        env.Run(editor_builders.make_editor_translations_header),
    )

    # Property translations
    tlist = glob.glob(env.Dir("#editor/translations/properties").abspath + "/*.po")
    env.Depends("#editor/property_translations.gen.h", tlist)
    env.CommandNoCache(
        "#editor/property_translations.gen.h",
        tlist,
        env.Run(editor_builders.make_property_translations_header),
    )

    # Documentation translations
    tlist = glob.glob(env.Dir("#doc/translations").abspath + "/*.po")
    env.Depends("#editor/doc_translations.gen.h", tlist)
    env.CommandNoCache(
        "#editor/doc_translations.gen.h",
        tlist,
        env.Run(editor_builders.make_doc_translations_header),
    )

    # Extractable translations
    tlist = glob.glob(env.Dir("#editor/translations/extractable").abspath + "/*.po")
    tlist.extend(glob.glob(env.Dir("#editor/translations/extractable").abspath + "/extractable.pot"))
    env.Depends("#editor/extractable_translations.gen.h", tlist)
    env.CommandNoCache(
        "#editor/extractable_translations.gen.h",
        tlist,
        env.Run(editor_builders.make_extractable_translations_header),
    )

    env.add_source_files(env.editor_sources, "*.cpp")
    env.add_source_files(env.editor_sources, gen_exporters)

    SConscript("debugger/SCsub")
    SConscript("export/SCsub")
    SConscript("gui/SCsub")
    SConscript("icons/SCsub")
    SConscript("import/SCsub")
    SConscript("plugins/SCsub")
    SConscript("project_manager/SCsub")
    SConscript("themes/SCsub")

    lib = env.add_library("editor", env.editor_sources)
    env.Prepend(LIBS=[lib])
